<?php
/**
 * @package		Joomla.Administrator
 * @subpackage	com_weblinks
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('JPATH_BASE') or die;

/**
 * Supports an HTML select list of provinces
 *
 * @package		Joomla.Administrator
 * @subpackage	com_weblinks
 * @since		1.6
 */
class JFormFieldProvinces extends JFormField
{
	/**
	 * The form field type.
	 *
	 * @var		string
	 * @since	1.6
	 */
	protected $type = 'Provinces';

	/**
	 * Method to get the field input markup.
	 *
	 * @return	string	The field input markup.
	 * @since	1.6
	 */
	protected function getInput()
	{
		// Initialize variables.
		$db = JFactory :: getDBO();
		$html = array();
		$attr = '';
		

		// Initialize some field attributes.
		$class = $this->element['class'] ? ' class="'.(string) $this->element['class'].'"' : '';
		$size = $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';

		// Initialize JavaScript field attributes.
		$attr .= $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';

		// Get some field values from the form.
		$p_id	= (int) JRequest::getCmd('p_id');
		
		//Get p_id from c_id if provided
		if($p_id == 0)
		{
			$c_id = JRequest::getInt('c_id');
			if($c_id != 0)
			{
				$query = 'select p_id FROM #__congregation_casa WHERE c_id=' . $c_id;
				$db->setQuery($query);
				$result = $db->loadObject();
				$p_id = $result->p_id;
			}

		}
		
		
		if($p_id == 0)
		{
		
			$app = JFactory::getApplication('administrator');
			$p_id = $app->getUserStateFromRequest('com_congregation.houses.filter.p_id', 'filter_p_id');

		}
		
		// Build the query for the ordering list.
		$query = 'SELECT p_id , p_name ' .
				' FROM #__congregation_province' .
				' ORDER BY ordering';

		// Create a read-only list (no name) with a hidden input to store the value.
		$db->setQuery($query);
		$result = $db->loadObjectList();
		$options = array();
		$options[] = JHTML::_('select.option', '0', 'Select Name');
		foreach($result as $row)
		{
			$options[] = JHTML::_('select.option', $row->p_id, $row->p_name);
		}
		$dropdown = JHTML::_('select.genericlist', $options, $this->name, $class, 'value', 'text', $p_id);
		return $dropdown; 
		
	}
}
